import Vue from 'vue'
import VueRouter, { RouteConfig } from 'vue-router'
import store from '@/store'
import { GetClientInfo } from '@/apis/api'

const loadRoute = async () : Promise<VueRouter> => {
  Vue.use(VueRouter)
  const clientResult = await GetClientInfo()
  let route = 'landscape'
  if (clientResult.data?.Code === 200) {
    store.commit('updateClient', clientResult.data.Data)
    if (clientResult.data.Data?.Entity?.Theme?.Route) {
      route = clientResult.data.Data.Entity.Theme.Route
    }
  }
  const routes: Array<RouteConfig> = [
    {
      path: '/',
      redirect: `/${route}`
    },
    {
      path: '/portrait',
      redirect: '/portrait/home',
      component: () => import(/* webpackChunkName: 'PortraitIndex' */ '@/views/portrait/index.vue'),
      children: [
        {
          path: '/portrait/home',
          component: () => import(/* webpackChunkName: 'PortraitHome' */ '@/views/portrait/home.vue')
        },
        {
          path: '/portrait/card',
          component: () => import(/* webpackChunkName: 'PortraitCard' */ '@/views/portrait/common/card.vue')
        },
        {
          path: '/portrait/read',
          component: () => import(/* webpackChunkName: 'PortraitRead' */ '@/views/portrait/common/read.vue')
        },
        {
          path: '/portrait/create',
          component: () => import(/* webpackChunkName: 'PortraitCreate' */ '@/views/portrait/create.vue')
        },
        {
          path: '/portrait/detail',
          component: () => import(/* webpackChunkName: 'PortraitDetail' */ '@/views/portrait/detail.vue')
        }
      ]
    },
    {
      path: '/reservation',
      redirect: '/reservation/home',
      component: () => import(/* webpackChunkName: 'ReservationIndex' */ '@/views/reservation/index.vue'),
      children: [
        {
          path: '/reservation/home',
          component: () => import(/* webpackChunkName: 'ReservationHome' */ '@/views/reservation/home.vue')
        },
        {
          path: '/reservation/doctor',
          component: () => import(/* webpackChunkName: 'ReservationRoom' */ '@/views/reservation/doctor.vue')
        },
        {
          path: '/reservation/sign',
          component: () => import(/* webpackChunkName: 'ReservationRoom' */ '@/views/reservation/sign.vue')
        },
        {
          path: '/reservation/detail',
          component: () => import(/* webpackChunkName: 'ReservationDetail' */ '@/views/reservation/detail.vue')
        },
        {
          path: '/reservation/add-input',
          component: () => import(/* webpackChunkName: 'ReservationAddInput' */ '@/views/reservation/add-input.vue')
        },
        {
          path: '/reservation/add-ercode',
          component: () => import(/* webpackChunkName: 'InspectionIndexAddErCode' */ '@/views/reservation/add-ercode.vue')
        },
        {
          path: '/reservation/register-pay',
          component: () => import(/* webpackChunkName: 'ReservationRegisterPay' */ '@/views/reservation/register-pay.vue')
        }
      ]
    },
    {
      path: '/inspection',
      redirect: '/inspection/home',
      component: () => import(/* webpackChunkName: 'InspectionIndex' */ '@/views/inspection/index.vue'),
      children: [
        {
          path: '/inspection/home',
          component: () => import(/* webpackChunkName: 'InspectionIndexHome' */ '@/views/inspection/home.vue')
        },
        {
          path: '/inspection/room',
          component: () => import(/* webpackChunkName: 'InspectionIndexRoom' */ '@/views/inspection/room.vue')
        },
        {
          path: '/inspection/inspection-list',
          component: () => import(/* webpackChunkName: 'InspectionIndexInspectionList' */ '@/views/inspection/inspection-list.vue')
        },
        {
          path: '/inspection/detail',
          component: () => import(/* webpackChunkName: 'InspectionIndexDetail' */ '@/views/inspection/detail.vue')
        },
        {
          path: '/inspection/add-input',
          component: () => import(/* webpackChunkName: 'InspectionIndexAddInput' */ '@/views/inspection/add-input.vue')
        },
        {
          path: '/inspection/add-ercode',
          component: () => import(/* webpackChunkName: 'InspectionIndexAddErCode' */ '@/views/inspection/add-ercode.vue')
        },
        {
          path: '/inspection/inspection-pay',
          component: () => import(/* webpackChunkName: 'InspectionPay' */ '@/views/inspection/inspection-pay.vue')
        },
        {
          path: '/inspection/input-code',
          component: () => import(/* webpackChunkName: 'InputCode' */ '@/views/inspection/input-code.vue')
        }
      ]
    }
  ]
  const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
  })
  return router
}

export default loadRoute
